// ~/plugins/router.ts
import { useLayoutStore } from "~/stores/layout"
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.$router.beforeEach((to:any, from:any, next:any) => {
    const publicPaths = ['/signin', '/signup', '/'];
    const authStore = useLayoutStore();
    console.log(to.path)
    if(publicPaths.includes(to.path)){
      next()
    }else{
      // const authStore = useLayoutStore();
      console.log("token++",localStorage.getItem('token'))
      const savedToken = localStorage.getItem('token');
      if (savedToken) {
        authStore.setToken(savedToken);
        next();
      }else{
        next('/signin');
      }
      // console.log("authStore.token",authStore)
      // console.log("authStore.token",authStore.token)
      // if (!authStore.token) {
      // next('/signin');
      //   console.log("没有token")
      // } else {
      //   console.log("有token")
      //   next();
      // }
    }
  });
});